package gweb

import (
	"fmt"

	"github.com/spf13/cobra"
)

// 数据库迁移
func migrate(app *GinApp) *cobra.Command {
	var env string
	var command = &cobra.Command{
		Use:   "migrate",
		Short: "Migrate db models",
		Run: func(cmd *cobra.Command, args []string) {
			if env != "" {
				app.InitEnv(env)
			}
			if len(app.Models) > 0 {
				app.MigrateDbModels()
			}
		},
	}
	command.PersistentFlags().StringVar(&env, "env", "prod", "执行环境: prod/dev/test")
	return command
}

func serve(app *GinApp) *cobra.Command {
	var env string
	var host string
	var port int
	var command = &cobra.Command{
		Use:   "serve",
		Short: "Start the server",
		Run: func(cmd *cobra.Command, args []string) {
			if env != "" {
				app.InitEnv(env)
			}

			// // 系统变量
			// app.useVariable()
			// // 系统维护
			// app.useHotMaintain()
			// // 任务管理
			// app.useTaskManager()
			addr := ""
			if host != "" && port > 0 {
				addr = fmt.Sprintf("%s:%d", host, port)
			}
			app.Run(addr)
		},
	}
	command.PersistentFlags().StringVar(&env, "env", "prod", "执行环境: prod/dev/test")
	command.PersistentFlags().StringVar(&host, "host", "", "服务启动的host地址")
	command.PersistentFlags().IntVar(&port, "port", 0, "服务启动端口")

	return command
}

func (app *GinApp) RunCommand(f func(cmdargs map[string]any)) {

}
